#include "rtea.h"

void rtea_encrypt(unsigned int* data, int length, const unsigned int* key)
{
  for (int i = 0; i < length; i+=2){
    rtea_encrypt_block(&data[i], key);
  }
}

void rtea_decrypt(unsigned int* data, int length, const unsigned int* key)
{
  for (int i = 0; i < length; i+=2){
    rtea_decrypt_block(&data[i], key);
  }
}


void rtea_encrypt_block(unsigned int* data, const unsigned int* key)
{
  for (long r = 0; r < 64; r++) {
    data[1] = data[1] + data[0] + ((data[0]<<6)^(data[0]>>8)) + (key[r%8] + r);
    r++;
    data[0] += data[1] + ((data[1]<<6)^(data[1]>>8)) + (key[r%8] + r);
  }
}

void rtea_decrypt_block(unsigned int* data, const unsigned int* key)
{
  for (long r = 63; r >= 0; r--){
    data[0] -= data[1] + ((data[1]<<6)^(data[1]>>8)) + (key[r%8] + r);
    r--;
    data[1] -= data[0] + ((data[0]<<6)^(data[0]>>8)) + (key[r%8] + r);
  }
}
